package com.cryptooj.project.manager;

import com.cryptooj.project.config.CosClientConfig;
import com.qcloud.cos.COSClient;
import com.qcloud.cos.model.PutObjectRequest;
import com.qcloud.cos.model.PutObjectResult;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.File;
import java.io.InputStream;

/**
 *  Cos（COS，Cloud Object Storage） 对象存储操作
 *  （不是跨域Cors：Cross-Origin Resource Sharing）
 *  操作腾讯云对象存储的工具类。
 *  封装了与 COS 交互的核心方法，简化了文件上传等操作
 * */
@Component
public class CosManager {

    @Resource
    private CosClientConfig cosClientConfig;

    @Resource
    private COSClient cosClient;

    /**
     *  上传对象
     * */
    public PutObjectResult putObject(String key, String localFilePath) {
        // new 上传请求
        PutObjectRequest putObjectRequest = new PutObjectRequest(
                cosClientConfig.getBucket(),
                key,
                new File(localFilePath));
        return cosClient.putObject(putObjectRequest);
    }

    /**
     *  上传对象(重载)
     * */
    public PutObjectResult putObject(String key, File file) {
        // new 上传请求
        PutObjectRequest putObjectRequest = new PutObjectRequest(
                cosClientConfig.getBucket(),
                key,
                file);
        return cosClient.putObject(putObjectRequest);
    }
}
